<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        新建
      </el-button>
      <div style="width: 20%;float: right;">
        <el-input
          v-model="listQuery.name"
          style="width: 70%;float: left;"
          size="mini"
          placeholder="输入关键字搜索"
        />
        <svg-icon style="width: 30%;float: left;" icon-class="search" @click="getList"/>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="编号"
        prop="id"
        sortable="custom"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="工号" prop="employeeNo" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.employeeNo }}</span>
        </template>
      </el-table-column>

      <el-table-column label="名字" prop="name" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属学院" prop="orgName" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.orgName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否管理全级" prop="isAllGrade" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.isAllGrade ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理年级" prop="grades" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ listOut(row.grades) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="管理社区" prop="communities" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ listOut(row.communities) }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row,$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="left"
        label-width="90px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item v-if="dialogStatus==='create'" label="名字">
          <el-col :span="12">
            <el-input v-model="temp.name"/>
          </el-col>
        </el-form-item>
        <el-form-item v-if="dialogStatus==='create'" label="性别">
          <el-col :span="12">
            <el-select v-model="temp.sex" placeholder="请选择">
              <el-option
                v-for="item in sexs"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item v-if="dialogStatus==='create'" label="角色">
          <el-col :span="12">
            <el-select v-model="temp.roleId" placeholder="请选择">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item  label="所属学院">
          <el-col :span="12">
            <el-select v-model="temp.orgId" placeholder="请选择">
              <el-option
                v-for="item in orgs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="工号">
          <el-col :span="12">
            <el-input v-model="temp.employeeNo"/>
          </el-col>
        </el-form-item>
        <el-form-item label="是否管理全级">
          <el-col :span="12">
            <el-switch
              v-model="temp.isAllGrade"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-col>
        </el-form-item>

        <el-form-item  label="管理的年级">
          <el-col :span="12">
            <el-select v-model="temp.gradeIds" multiple placeholder="请选择">
              <el-option
                v-for="item in grades"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item  label="管理的社区">
          <el-col :span="12">
            <el-select v-model="temp.communityIds" multiple placeholder="请选择">
              <el-option
                v-for="item in communities"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {deleteEmployee, listRole, listEmployee, patchEmployee, postEmployee} from '@/api/dorm-user'
import Pagination from '@/components/Pagination'
import {listGrade, listOrg} from "@/api/org";
import {listCommunity} from "@/api/dorm";

export default {
  name: 'Employee',
  components: {Pagination},
  data() {
    return {
      // 表格数据
      list: [],
      listLoading: false,
      identities: ['学生', '教职工', '其他'],
      statuses: ['正常', '禁用'],
      // 表单数据
      temp: {
        id: undefined,
        name: '',
        sex: '',
        employeeName: '',
        phone: '',
        identity: undefined,
        roleId: undefined,
        status: undefined,
        password: '',
        employeeNo:'',
        isAllGrade:false,
        orgId:undefined,
        communityIds:[],
        gradeIds:[]
      },
      roles: [],
      orgs: [],
      grades:[],
      communities:[],
      sexs: ['男', '女'],
      // 分页数据
      listQuery: {
        current: 1,
        pageSize: 20,
        name: undefined,
        title: undefined
      },
      total: 0,
      // 固定数据
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      }
    }
  },
  created() {
    this.getList()
    this.getRoles()
    this.getCommunities()
    this.getGrades()
    this.getOrgs()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 改变
      listEmployee(this.listQuery).then(response => {
        this.list = response.data
        console.log(response.meta.total)
        this.total = response.meta.total
        console.log(this.total)
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.listLoading = false
    },
    // 改变
    getRoles() {
      listRole(this.listQuery).then(response => {
        this.roles = response.data
      })
    },
    getOrgs() {
      listOrg({ typ: 0 }).then(response => {
        this.orgs = response.data
      })
    },
    getGrades() {
      listGrade().then(response => {
        this.grades = response.data
      })
    },
    getCommunities() {
      listCommunity().then(response => {
        this.communities = response.data
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        employeename: '',
        phone: '',
        identity: undefined,
        roleId: undefined,
        status: undefined,
        password: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      postEmployee(this.temp).then((response) => {
        this.getList()
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '添加成功!',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    updateData() {
      patchEmployee(this.temp).then(() => {
        this.getList()
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '更新成功!',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleDelete(row, index) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteEmployee(row.id).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功!',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    listOut(list) {
      let str = ''
      for (const listKey in list) {
        str = str + listKey + ','
      }
      if (str.length === 0) {
        return str
      }
      return str.slice(0, str.length - 1)
    }
  }
}
</script>

<style scoped>

</style>
